package com.jxd.ordersys.controller;

import com.jxd.ordersys.model.Emp;
import com.jxd.ordersys.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EmpController
 * @Description TODO
 * @Author 王云超
 * @Date 2024/9/19 16:51
 * @Version 1.0
 */
@RestController
public class EmpController {
    @Autowired
    IEmpService empService;

    @RequestMapping("getEmpByPage")
    public Map<String,Object> getEmpByPage(@RequestBody Map<String,Object> queryMap){
        Integer page = (Integer) queryMap.get("page");
        Integer limit = (Integer) queryMap.get("limit");
        String empName = (String) queryMap.get("empName");
        Integer deptId =queryMap.get("deptId")==null?0:(Integer) queryMap.get("deptId");
        List<Map<String,Object>> emps = empService.getEmpByPage(page,limit,empName,deptId);
        int count = empService.getEmpCount(empName,deptId);
        Map<String,Object> response = new HashMap<>();
        response.put("emps",emps);
        response.put("count",count);
        return  response;
    }


    /**
     * 查询同部门当天未订餐的员工
     * @return 员工信息
     */
    @RequestMapping("/getEmpNotOrder/{deptId}")
    public List<Emp> getEmpNotOrder(@PathVariable("deptId") int deptId){
        return empService.getEmpNotOrder(deptId);
    }

    /**
     * 根据员工id查询员工
     * @param empId 员工id
     * @return 员工信息
     */
    @RequestMapping("/getById/{empId}")
    public Emp getById(@PathVariable("empId") int empId){
        return empService.getEmpById(empId);
    }

    /**
     * 添加员工
     * @param emp 员工信息
     * @return 添加员工结果信息
     */
    @RequestMapping("/addEmp")
    public String addEmp(@RequestBody Emp emp) {
        boolean addFlag = empService.addEmp(emp);
        if (addFlag) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 编辑员工信息
     * @param emp 员工信息
     * @return 编辑员工结果信息
     */
    @RequestMapping("/editEmp")
    public String editEmp(@RequestBody Emp emp) {
        boolean editFlag = empService.editEmp(emp);
        if (editFlag) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 删除员工
     * @param empIds 员工编号列表
     * @return 删除员工结果信息
     */
    @RequestMapping("/delEmp")
    public String delEmp(@RequestBody List<Integer> empIds) {
        boolean delFlag = empService.delEmpByIds(empIds);
        if (delFlag) {
            return "success";
        }
        return "error";
    }
}